<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Base class for Grid.
 * 
 * Much the same as the List, just has different css style?
 * 
 * Widgets for displaying a grid sytle or same.
 * Includes a grid widget and its specific item widget.
 * 
 * 用来显示一个网格的控件.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.1
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_widget.php';

// Definitions.
// After it, no item would display; Might be LARGER than the 'XXX_PERPAGE'
define('GRID_MAX_ITEM_COUNT',		90);

define('GRID_DEFAULT_HEIGHT',		180);
define('GRID_DEFAULT_WIDTH',		180);


/**
 * Widget for displaying Grid.
 */
class Grid extends Widget
{
	/**
	 * Item object that constructed the grid content.
	 */
	var $obj = null;
	
	/**
	 * Constructor.
	 */
	function __construct(&$obj, $out = null)
	{
		parent::__construct($out);
		$this->obj =& $obj;
	}
	
	/**
	 * Starts a grid.
	 * The notice or some sayings of the grid. Might some labels.
	 * 
	 * Might be overrided.!
	 */
	function showStart()
	{
		$this->out->elementStart('div', array('class' => 'cls_div_grid'));
		
		// The grid title.
		$this->out->instruction('网格');
	}
	
	/**
	 * Ends a grid.
	 */
	function showEnd()
	{
		$this->out->elementEnd('div');
	}
	
	/**
	 * Show the grid
	 * 
	 * @return int, count of item listed.
	 */
	function show()
	{
		$this->showStart();
		
		$itemcnt = 0;
		while ($this->obj->fetch() && $itemcnt <= GRID_MAX_ITEM_COUNT) {
			++$itemcnt;
			if ($itemcnt > GRID_MAX_ITEM_COUNT) {
				break;
			}
			
			try {
				$item = $this->newListItem($this->obj);
				$item->show();
			} catch (Exception $e) {
				// FIXME: Log?
				continue;
			}
		}
		
		$this->showEnd();
		
		return $itemcnt;
	}
	
	/**
	 * Returns a new grid item for the current object.
	 */
	function newListItem(&$obj)
	{
		return new BaseGridItem($obj, $this->out);
	}
}

/**
 * Widget for displaying a single item with some options.
 */
class BaseGridItem extends Widget
{
	/**
	 * The item this item will show().
	 */
	var $obj = null;
	
	/**
	 * Constructor.
	 */
	function __construct($obj, $out = null)
	{
		parent::__construct($out);
		
		$this->obj =& $obj;
	}
	
	/**
	 * Recipe function for displaying a single item.
	 */
	function show()
	{
		if (empty($this->obj)) {
			return;
		}
		
		$this->prepareData();
		$this->showStart();
		$this->showDataInfo();
		$this->showOptions();
		$this->showEnd();
	}
	
	/**
	 * Do some data preparation.
	 */
	function prepareData()
	{
	}
	
	/**
	 * Start a single item.
	 * Overriding.
	 */
	function showStart()
	{
		$this->out->element('div', array('class' => 'cls_div_griditem'));
		
		$this->out->elementStart('fieldset');
		$legend = 'id: ' . $this->obj->id;
		$this->out->element('legend', null, $legend);
	}
	
	/**
	 * Finish the note news item.
	 */
	function showEnd()
	{
		$this->out->elementEnd('fieldset');
		$this->out->elementEnd('div');
	}
	
	/**
	 * Overriding.
	 */
	function showDataInfo()
	{
		// <div><img></div>
	}
	
	/**
	 * Overriding.
	 */
	function showOptions()
	{
		// <div><a></div>
	}
}

?>